package com.atXiao.gpt.service.impl;

import cn.hutool.http.Header;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.atXiao.gpt.entity.GPTData;
import com.atXiao.gpt.entity.GPTImageResponse;
import com.atXiao.gpt.entity.GPTResponse;
import com.atXiao.gpt.service.ChartGptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.atXiao.gpt.entity.Message;

/**
 * @author lixiaoxiao
 */
@Service("chartGPTService")
public class ChartGptServiceImpl implements ChartGptService {
    @Value("${ChatGPT.variables.apiKey}")
    private String apiKey;
    @Value("${ChatGPT.variables.maxTokens}")
    private String maxTokens;
    @Value("${ChatGPT.variables.model}")
    private String model;
    @Value("${ChatGPT.variables.temperature}")
    private String temperature;

    /**
     * 代理主机
     */
    private String proxyHost = "127.0.0.1";

    /**
     * 代理端口
     */
    private Integer proxyPort = 63649;


    @Override
    public String send(String prompt) {
        JSONObject bodyJson = new JSONObject();
        Message message = new Message();
        message.setContent(prompt);
        message.setRole("system");
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(message);
        bodyJson.put("messages", messages);
        bodyJson.put("model", model);
        bodyJson.put("max_tokens", Integer.parseInt(maxTokens));
        bodyJson.put("temperature", Double.parseDouble(temperature));
        Map<String, Object> headMap = new HashMap<>();
//        headMap.put("Authorization", "Bearer " + apiKey);

        // 定义代理对象
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        HttpResponse httpResponse =
                // 官网请求，没梯子不能访问
                // HttpUtil.createPost("https://api.openai.com/v1/chat/completions")
                // 使用代理地址 https://api.openai-proxy.com/
                HttpUtil.createPost("https://openkey.cloud/v1/chat/completions")
                        .header(Header.AUTHORIZATION, "Bearer " + apiKey)
                        .header(Header.CONTENT_TYPE, "application/json")
                        .setProxy(proxy)
                        .body(JSONUtil.toJsonStr(bodyJson)).execute();
        String resStr = httpResponse.body();

        GPTResponse gptResponse = JSONUtil.toBean(resStr, GPTResponse.class);

        return gptResponse.getChoices().get(0).getMessage().getContent().replaceAll("\\n", "");

    }

    @Override
    public List<GPTData> sendImg(String prompt) {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("prompt", prompt);
        bodyJson.put("n", 2);
        bodyJson.put("size", "1024x1024");
        Map<String, Object> headMap = new HashMap<>();
        headMap.put("Authorization", "Bearer " + apiKey);
        HttpResponse httpResponse =
                // 官网请求，没梯子不能访问
                // HttpUtil.createPost("https://api.openai.com/v1/chat/completions")
                // 使用代理地址 https://api.openai-proxy.com/
                HttpUtil.createPost("https://openkey.cloud/v1/images/generations")
                        .header(Header.AUTHORIZATION, "Bearer " + apiKey)
                        .header(Header.CONTENT_TYPE, "application/json")
                        .body(JSONUtil.toJsonStr(bodyJson)).execute();
        String resStr = httpResponse.body();

        GPTImageResponse gptResponse = JSONUtil.toBean(resStr, GPTImageResponse.class);

        List<GPTData> data = gptResponse.getData();

        return data;

    }
}
